﻿using HAF.LogUtility.Except;
using MediaUtility.Enum;
using System;
using System.Runtime.InteropServices;

namespace MediaUtility
{
    public class MediaAssistant : IDisposable
    {
        //非托管Dll调用方法
        //托管代码是基于.net平台开发的，非托管代码主要是基于win 32平台开发的DLL，activeX的组件
        //非托管Dll无法直接引用

        #region 外部库引用 
        [DllImport("MediaInfo.dll")]
        public static extern IntPtr MediaInfo_New();

        [DllImport("MediaInfo.dll", CharSet = CharSet.Unicode)]
        public static extern int MediaInfo_Open(IntPtr handle, string path);

        [DllImport("MediaInfo.dll", CharSet = CharSet.Unicode)]
        public static extern IntPtr MediaInfo_Option(IntPtr handle, string option, string value);

        [DllImport("MediaInfo.dll")]
        public static extern IntPtr MediaInfo_Inform(IntPtr handle, int reserved);

        [DllImport("MediaInfo.dll")]
        public static extern int MediaInfo_Close(IntPtr handle);

        [DllImport("MediaInfo.dll")]
        public static extern void MediaInfo_Delete(IntPtr handle);

        [DllImport("MediaInfo.dll", CharSet = CharSet.Unicode)]
        public static extern IntPtr MediaInfo_Get(IntPtr handle, StreamKind streamKind,
            int stream, string parameter, InfoKind infoKind, InfoKind searchKind);

        [DllImport("MediaInfo.dll", CharSet = CharSet.Unicode)]
        public static extern int MediaInfo_Count_Get(IntPtr handle, StreamKind streamKind, int stream);
        #endregion 外部库引用

        IntPtr Handle;

        /// <summary>
        /// 构建函数
        /// </summary>
        /// <param name="path"></param>
        /// <exception cref="Exception"></exception>
        public MediaAssistant(string path)
        {
            if ((Handle = MediaInfo_New()) == IntPtr.Zero)
                throw new Exception("Error MediaInfo_New");

            if (MediaInfo_Open(Handle, path) == 0)
                throw new RunException(RunCode.FileNotFound, "Error MediaInfo_Open");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="streamKind"></param>
        /// <param name="stream"></param>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public string GetInfo(StreamKind streamKind, int stream, string parameter)
        {
            return Marshal.PtrToStringUni(MediaInfo_Get(Handle, streamKind, stream, parameter,
                InfoKind.Text, InfoKind.Name)) ?? "";
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="complete"></param>
        /// <param name="rawView"></param>
        /// <returns></returns>
        public string GetSummary(bool complete, bool rawView)
        {
            MediaInfo_Option(Handle, "Language", rawView ? "raw" : "");
            MediaInfo_Option(Handle, "Complete", complete ? "1" : "0");
            return Marshal.PtrToStringUni(MediaInfo_Inform(Handle, 0)) ?? "";
        }

        bool Disposed;

        /// <summary>
        /// 
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        public void Dispose()
        {
            if (!Disposed)
            {
                if (Handle != IntPtr.Zero)
                {
                    MediaInfo_Close(Handle);
                    MediaInfo_Delete(Handle);
                }

                Disposed = true;
            }
        }

        /// <summary>
        /// 析构函数
        /// </summary>
        ~MediaAssistant()
        {
            Dispose();
        }
    }
}
